# coding=utf-8
from ToolsConfig import *

CreatedTools = {}
ButtonTouchMap = {
    ButtonTouchTypeEnum.TouchDown: 'SetButtonTouchDownCallback',
    ButtonTouchTypeEnum.TouchUp: 'SetButtonTouchUpCallback'
}


def RegisterScreenFastly(cls, UiScreenDef):
    """
    快速注册Ui界面,其中:
    --namespace为ToolsConfig.MOD_NAME
    --uiKey为类名
    """
    clientApi.RegisterUI(
        MOD_NAME, cls.__name__,
        '{}.{}'.format(cls.__module__, cls.__name__),
        UiScreenDef
    )


class ScreenTools(object):
    """
    界面工具
    """
    autoRegister = {}

    @classmethod
    def AutoRegister(cls, uiScreenDef):
        def register(screenCls):
            ScreenTools.autoRegister[uiScreenDef] = screenCls
            return screenCls

        return register

    @classmethod
    def GetTools(cls, tag):
        """
        绑定UI类
        :param tag:界面定义路径
        :return:
        """
        tag = tag + MOD_NAME + MOD_VERSION
        instance = CreatedTools.get(tag)
        if instance is None:
            instance = cls()
            CreatedTools[tag] = instance
        return instance

    def __init__(self):
        self.buttonTouchBackFunc = {}
        self.bindingInstance = None

    def Binding(self, screenInstance):
        self.bindingInstance = screenInstance
        self.FirstAddTouchBackFunc()

    def AddBackFuncForButton(self, touchType, backFuncs):
        pass

    def ButtonCallFuncBackBinder(self, touchType, buttonPath):
        """
        绑定按钮回调函数
        :param buttonPath:按钮路径
        :param touchType:接触类型,位于ToolsConfig.ButtonTouchTypeEnum
        """

        def _binding(func):
            items = self.buttonTouchBackFunc.get(touchType, set())
            if not items: self.buttonTouchBackFunc[touchType] = items
            items.add((buttonPath, func.__name__))
            if self.bindingInstance:
                button = self.bindingInstance.GetBaseUIControl(buttonPath)
                if button:
                    button = button.asButton()
                    button.AddTouchEventParams()
                    getattr(button, ButtonTouchMap[touchType])(getattr(self.bindingInstance, func.__name__))
            return func

        return _binding

    def FirstAddTouchBackFunc(self):
        if self.bindingInstance:
            for touchType, backFuncs in self.buttonTouchBackFunc.items():
                for buttonPath, backFuncName in backFuncs:
                    button = self.bindingInstance.GetBaseUIControl(buttonPath)
                    if button:
                        button = button.asButton()
                        button.AddTouchEventParams()
                        getattr(button, ButtonTouchMap[touchType])(getattr(self.bindingInstance, backFuncName))

    @staticmethod
    def CollectionViewBinder(bindFlag, collectionName, bindingName, func):
        """
        绑定数据函数,为执行函数添加一个新的参数:collectionName.此collectionName与形参collectionName一致

        这是一个逆天工具,改造网易屎山

        若不传入cls,确保在binding之后调用

        :param func:必须是bound函数,即self.func
        :param bindFlag:
        :param collectionName:
        :param bindingName:
        :return:
        """

        def __(*args):
            return func(collectionName, args[1])

        cls = func.im_class
        funcBindingName = collectionName + bindingName
        setattr(cls, funcBindingName, __)
        newFunc = getattr(cls, funcBindingName).im_func
        newFunc.__name__ = funcBindingName
        viewBinder.binding_collection(bindFlag, collectionName, bindingName)(newFunc)
